package com.brotherly.framework.util;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.OutputStream;
import java.util.Vector;

import android.graphics.Point;

public class PolylineCodec
{
    public static byte[] encodePolyline(Vector<Point> paramVector)
    {
        Object localObject;
        byte[] arrayOfByte = (byte[]) null;
        try
        {
            if ((paramVector == null) || (paramVector.size() <= 0))
                return arrayOfByte;

            localObject = new ByteArrayOutputStream();
            new DataOutputStream((OutputStream) localObject);
            for (int i = 0; i < paramVector.size(); ++i)
            {
                Point localPoint1 = (Point) paramVector.elementAt(i);
                if (i == 0)
                {
                    a((ByteArrayOutputStream) localObject, localPoint1.x);
                    a((ByteArrayOutputStream) localObject, localPoint1.y);
                } else
                {
                    Point localPoint2 = (Point) paramVector.elementAt(i - 1);
                    int j = localPoint1.x - localPoint2.x;
                    int k = localPoint1.y - localPoint2.y;
                    a((ByteArrayOutputStream) localObject, j);
                    a((ByteArrayOutputStream) localObject, k);
                }
            }
            arrayOfByte = ((ByteArrayOutputStream) localObject).toByteArray();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return arrayOfByte;
    }

    private static int a(ByteArrayOutputStream bytearrayoutputstream, int i)
    {
        int j = 0;
        int k = i < 0 ? (-i << 1) + 1 : i << 1;
        int l = 0;
        for (; k >= 128; k >>= 7)
        {
            l = (k & 127) << 1;
            bytearrayoutputstream.write(l);
            j++;
        }

        l = ((k & 127) << 1) + 1;
        bytearrayoutputstream.write(l);
        return ++j;
    }

    private static int[] a(byte abyte0[], int i, int j)
    {
        int ai[] = (int[]) null;
        if (abyte0 != null && abyte0.length > 0 && i >= 0 && i < abyte0.length && j <= abyte0.length)
        {
            int k = i;
            Vector<Integer> vector = new Vector<Integer>();
            while (k < j)
            {
                int i1 = 0;
                int j1 = 0;
                byte byte0;
                do
                {
                    int k1 = (byte0 = abyte0[k++]) >> 1 & 127;
                    j1 |= k1 << i1;
                    i1 += 7;
                } while ((byte0 & 1) == 0);
                int l1 = j1 >> 1;
                if ((j1 & 1) != 0)
                    l1 = -l1;
                vector.addElement(Integer.valueOf(l1));
            }
            ai = new int[vector.size()];
            for (int l = 0; l < ai.length; l++)
                ai[l] = ((Integer) vector.elementAt(l)).intValue();
        }
        return ai;
    }

    public static Vector<Point> decodePolyline(byte[] paramArrayOfByte)
    {
        return decodePolyline(paramArrayOfByte, 0, paramArrayOfByte.length);
    }

    public static Vector<Point> decodePolyline(byte[] paramArrayOfByte, int paramInt1, int paramInt2)
    {
        Vector<Point> localVector = new Vector<Point>();
        if ((paramArrayOfByte != null) && (paramArrayOfByte.length > 0) && (paramInt1 >= 0)
                && (paramInt1 < paramArrayOfByte.length)
                && (paramInt2 <= paramArrayOfByte.length))
        {
            int[] localObject;
            try
            {
                localObject = a(paramArrayOfByte, paramInt1, paramInt2);
                int i = 0;
                int j = 0;
                int k = 0;
                while (k < localObject.length)
                {
                    i += localObject[(k++)];
                    j += localObject[(k++)];
                    Point localPoint = new Point(i, j);
                    localVector.addElement(localPoint);
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
        return localVector;
    }

    public static void main(String[] paramArrayOfString)
    {
        Vector<Point> localVector1;
        (localVector1 = new Vector<Point>()).addElement(new Point(1163456, 3992345));
        for (int i = 0; i < 5600; ++i)
        {
            int j = (int) (Math.random() * 200.0D);
            int k = (int) (Math.random() * 200.0D);
            localVector1.addElement(new Point(1163456 + j, 3992345 + k));
        }
        byte[] arrayOfByte = encodePolyline(localVector1);
        System.out.println("[vPts]" + localVector1.size());
        System.out.println("[Enc]" + arrayOfByte.length);
        Vector<Point> localVector2 = decodePolyline(arrayOfByte);
        System.out.println("[Dec]" + localVector2.size());
    }
}
